var app = getApp(), that;
Page({

  /**
  * 页面的初始数据
  */
  data: {
    page: 1,
    rows: 10,
    category: [],
    addInfo: {},
    isLoadMore: true,
    isJzz: false,
    isJzwb: false,
    name: ''
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    console.log(options)
    that = this;
    that.setData({
      addInfo: wx.getStorageSync('ad_info')
    })
    // this.id = t.id;
  },

  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
  },

  /**
  * 生命周期函数--监听页面显示
  */
  onShow: function () {
    that.getShopInfo()
  },
  getShopInfo(e) {
    console.log(e)
    app.getData({
      url: app.api.getStoreData(that.data.page, that.data.rows),
      method: 'POST',
      data: {
        name: that.data.name,
        latitude: app.globalData.locationInfo.latitude,
        longitude: app.globalData.locationInfo.longitude
      },
      ok: function (res) {
        console.log(res)
        if (res.statusCode == 200 && res.errMsg == 'request:ok') {
          if (res.data.code == 1) {
            if (that.data.page == 1) {
              that.data.category = []
            }
            if (res.data.data.list.length < that.data.rows) {
              that.setData({
                category: that.data.category.concat(res.data.data.list),
                isLoadMore: false,
                isJzz:true
              })
            } else {
              that.setData({
                category: that.data.category.concat(res.data.data.list),
                isLoadMore: true,
                page: that.data.page + 1
              })
            }
          } else {
            wx.showToast({
              title: res.data.message,
            })
          }
        }
      }

    })
  },
  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {
  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {
  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    if (that.data.isLoadMore) {
      that.getShopInfo()
    } else {
      that.setData({
        isJzwb: true,
        isJzz: true
      })
      wx.showToast({
        title: '没有更多数据',
      })
    }
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (e) {
  },
  clickShopItem: function (e) {
    app.globalData.shopData = e.currentTarget.dataset.item
    app.globalData.companyId = e.currentTarget.dataset.item.companyId
    wx.setStorageSync( "shopData",  e.currentTarget.dataset.item)
    wx.setStorageSync( 'companyId', e.currentTarget.dataset.item.companyId)
    wx.switchTab({
      url: '/page/tabBar/index/index'
    })
  },
  toSearch(e) {
    setTimeout(function () {
      that.data.name = e.detail.detail.value
      that.data.page = 1
      that.getShopInfo()
    }, 800)
  }
})